package ipad.asia.controller;

import ipad.asia.config.EvaluateVO;
import ipad.asia.entity.Evaluate;
import ipad.asia.entity.Product;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.evaluate.EvaluateFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.utils.MyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class EvaluateController {
    @Autowired
    private EvaluateFeign evaluateFeign;
    @Autowired
    private ConsumerFeign consumerFeign;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private MyTime myTime;
    @RequestMapping("/evaluate/set")
    public String setEvaluate(@RequestParam(value = "orderId",required = false)String orderId,
                              @RequestParam(value = "productId",required = false)String productId,
                              @RequestParam(value = "content",required = false)String content,
                              @RequestParam(value = "level",required = false)String level,
                              HttpSession session, Model model, RedirectAttributes attr,
                              HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        if(evaluateFeign.findByOrderId(Integer.parseInt(orderId))!=null){
            System.out.println("你给我出来："+evaluateFeign.findByOrderId(Integer.parseInt(orderId)));
            attr.addAttribute("message","你已经评论过啦！");
            return "redirect:/order/finish";
        }
            if(content==null&&level==null){
                model.addAttribute("message","请给商品打上内容以及评分喔！");
                model.addAttribute("productId",productId);
                model.addAttribute("orderId",orderId);
                return "evaluate";
            }
        System.out.println("content:"+content);
        System.out.println("level:"+level);
            String consumerName= String.valueOf(session.getAttribute("user"));
            int shopId=productFeign.getById(Integer.parseInt(productId)).getShopId();
            evaluateFeign.launch(consumerName,shopId, Integer.parseInt(productId), Integer.parseInt(orderId),content,Integer.parseInt(level),myTime.LocalDateTime());
            model.addAttribute("message","评价成功！");
            return "redirect:/evaluate/my";
    }

    @RequestMapping("/evaluate/my")
    public String listEvaluate(HttpSession session,Model model){
        List<Evaluate> evaluates = evaluateFeign.findByConsumerName((String) session.getAttribute("user"));
        List<Product> products = productFeign.all();
        List<EvaluateVO> newEvaluates=new ArrayList<>();
        for(int i=0;i<evaluates.size();i++){
            for(int j=0;j<products.size();j++){
                if(evaluates.get(i).getProductId()==products.get(j).getId()){
                    EvaluateVO newEvaluate=new EvaluateVO();
                    newEvaluate.setEvaluate(evaluates.get(i));
                    newEvaluate.setProduct(products.get(i));
                    newEvaluates.add(newEvaluate);
                }
            }
        }
        System.out.println("我的评价："+newEvaluates);
        model.addAttribute("newEvaluates",newEvaluates);
        return "myEvaluate";
    }
}
